const mongoose = require('mongoose');

const model = mongoose.model('user', mongoose.Schema({
  subscribe: Number,
  openid: String,
  nickname: String,
  sex: Number,
  language: String,
  city: String,
  province: String,
  country: String,
  headimgurl: String,
  subscribe_time: Number,
  unionid: String,
  remark: String,
  groupid: String,
  tagid_list: Array,
  subscribe_scene: String,
  qr_scene: Number,
  qr_scene_str: String
}));

module.exports = class User {

  constructor() {
  }

  static save(opts) {

    return model.create(opts)
    .then(jawbreaker => {
      console.log('save success');

      // 重复数据
      model.aggregate([
        { $group: { _id : '$openid', count: { $sum : 1 } } },
        { $match: { count: { $gt : 1} } }
      ]).then(res1 => {
        console.log(res1)
      })

    }).catch(err => {
      console.log('save err', err);
    });
  }
}